package com.xbh.common.utils;

import com.xbh.common.constants.ConditionPeriodType;
import jodd.util.StringUtil;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

/**
 * 事件工具类
 */
public class DateTimeUtil {
    private static DateTimeFormatter DATETIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    public static LocalDate[] getStartAndEndDate(Integer period, String dateStart, String dateEnd) {
        LocalDateTime[] dateTimes = getStartAndEndTime(period, dateStart, dateEnd);
        LocalDate[] dates = {null, null};
        if(dateTimes[0] != null){
            dates[0] = dateTimes[0].toLocalDate();
        }
        if(dateTimes[1] != null){
            dates[1] = dateTimes[1].toLocalDate();
        }
        return dates;
    }

    public static LocalDateTime[] getStartAndEndTime(Integer period, String dateStart, String dateEnd) {
        if (period == null && StringUtil.isEmpty(dateStart) && StringUtil.isEmpty(dateEnd)) {
            period = ConditionPeriodType.MONTH;
        }
        LocalDate beginLocalDate = null;
        LocalDate endLocalDate = null;
        LocalDateTime begin = null;
        LocalDateTime end = null;
        if (period != null) {
            if (ConditionPeriodType.TODAY == period) {
                beginLocalDate = LocalDate.now();
                endLocalDate = beginLocalDate.plusDays(1);
                begin = LocalDateTime.of(beginLocalDate, LocalTime.MIN);
                end = LocalDateTime.of(endLocalDate, LocalTime.MIN);
            } else if (ConditionPeriodType.YESTERDAY == period) {
                endLocalDate = LocalDate.now();
                beginLocalDate = endLocalDate.minusDays(1);
                begin = LocalDateTime.of(beginLocalDate, LocalTime.MIN);
                end = LocalDateTime.of(endLocalDate, LocalTime.MIN);
            } else if (ConditionPeriodType.MONTH == period) {
                LocalDate now = LocalDate.now();
                beginLocalDate = LocalDate.of(now.getYear(), now.getMonth(), 1);
                endLocalDate = beginLocalDate.plusMonths(1);
                begin = LocalDateTime.of(beginLocalDate, LocalTime.MIN);
                end = LocalDateTime.of(endLocalDate, LocalTime.MIN);
            } else if (ConditionPeriodType.YEAR == period) {
                LocalDate now = LocalDate.now();
                beginLocalDate = LocalDate.of(now.getYear(), 1, 1);
                endLocalDate = beginLocalDate.plusYears(1);
                begin = LocalDateTime.of(beginLocalDate, LocalTime.MIN);
                end = LocalDateTime.of(endLocalDate, LocalTime.MIN);
            } else if (ConditionPeriodType.WEEK == period) {
                LocalDate now = LocalDate.now();
                beginLocalDate = now.with(DayOfWeek.MONDAY);
                endLocalDate = beginLocalDate.plusWeeks(1);
                begin = LocalDateTime.of(beginLocalDate, LocalTime.MIN);
                end = LocalDateTime.of(endLocalDate, LocalTime.MIN);
            } else if (ConditionPeriodType.LASTWEEK == period) {
                LocalDate now = LocalDate.now();
                endLocalDate = now.with(DayOfWeek.MONDAY);
                beginLocalDate = endLocalDate.minusWeeks(1);
                begin = LocalDateTime.of(beginLocalDate, LocalTime.MIN);
                end = LocalDateTime.of(endLocalDate, LocalTime.MIN);
            } else if (ConditionPeriodType.LAST_MONTH == period) {
                LocalDate now = LocalDate.now();
                LocalDate lastMonth = now.minusMonths(1);
                beginLocalDate = LocalDate.of(lastMonth.getYear(), lastMonth.getMonth(), 1);
                endLocalDate = LocalDate.of(now.getYear(), now.getMonth(), 1);
                begin = LocalDateTime.of(beginLocalDate, LocalTime.MIN);
                end = LocalDateTime.of(endLocalDate, LocalTime.MIN);
            } else if (ConditionPeriodType.ALL == period) {

            } else {
                LocalDate now = LocalDate.now();
                beginLocalDate = LocalDate.of(now.getYear(), now.getMonth(), 1);
                endLocalDate = beginLocalDate.plusMonths(1);
                begin = LocalDateTime.of(beginLocalDate, LocalTime.MIN);
                end = LocalDateTime.of(endLocalDate, LocalTime.MIN);
            }
            if(end != null){
                end =  LocalDateTime.of(end.minusDays(1).toLocalDate(), LocalTime.MAX);
            }
        } else {
            if (StringUtil.isNotEmpty(dateStart)) {
                begin = LocalDateTime.parse(dateStart, DATETIME_FORMATTER);
            }
            if (StringUtil.isNotEmpty(dateEnd)) {
                end = LocalDateTime.parse(dateEnd, DATETIME_FORMATTER).plusNanos(999999999);
            }
        }
        LocalDateTime[] localDateTimes = new LocalDateTime[]{begin, end};
        return localDateTimes;
    }
}
